package Web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.classic.Session;

import basak.HibernateUtil;

public class FirstPageServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String e_mail = req.getParameter("e-mail");
		String password = req.getParameter("Password");

		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();

			Query query = (Query) session
					.createQuery("Select u.e_mail from User u");

			List<String> mailList = new ArrayList<String>();
			mailList = query.list();
			boolean user=false;
			for (String x : mailList) {   
				if (x.equals(e_mail)) {
					user = true;
					Query info_query = session
							.createQuery("Select u.password from User u  where u.e_mail = :e_mail");
					info_query.setParameter("e_mail",e_mail);
					
					List pass = info_query.list();
					if (password.equals(pass.get(0))) {
						req.getRequestDispatcher("/HomePage.jsp").forward(req,
								resp);
						//return;
					}
				 else {
					 req.setAttribute("ERROR_MSG", "Kullanici adi sifre yanlis !");
						req.getRequestDispatcher("/error.jsp").forward(req, resp);
					return;
				}
					}
			}
			if(user==false) {
				req.setAttribute("ERROR_MSG", "Kullanici adi sifre yanlis !");
				req.getRequestDispatcher("/error.jsp").forward(req, resp);
			}
			
		} catch (Exception Ex) {
			Ex.printStackTrace();
		}

	}

}
